package com.ruoyi.common.net.webSoket;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.context.annotation.Configuration;
import org.springframework.messaging.simp.config.MessageBrokerRegistry;
import org.springframework.web.socket.config.annotation.EnableWebSocketMessageBroker;
import org.springframework.web.socket.config.annotation.StompEndpointRegistry;
import org.springframework.web.socket.config.annotation.WebSocketMessageBrokerConfigurer;
import org.springframework.web.socket.config.annotation.WebSocketTransportRegistration;
import org.springframework.web.socket.server.HandshakeInterceptor;

/**
 * @author xumh
 * @title: WebSocketConfig
 * @projectName ruoyi
 * @description: TODO
 * @date 2024/12/1813:55
 */

@Configuration
@ConditionalOnProperty(
        name = {"spring.websocket.enable"},
        matchIfMissing = false,
        havingValue = "true"
)
@EnableWebSocketMessageBroker
public class WebSocketConfig implements WebSocketMessageBrokerConfigurer {
    @Autowired
    private HandshakeInterceptor[] handshakeInterceptors;

    public WebSocketConfig() {
    }

    public void registerStompEndpoints(StompEndpointRegistry registry) {
        registry.addEndpoint(new String[]{"/ws/socket"}).setAllowedOrigins(new String[]{"*"}).withSockJS().setInterceptors(this.handshakeInterceptors);
    }

    public void configureMessageBroker(MessageBrokerRegistry registry) {
        registry.enableSimpleBroker(new String[]{"/topic", "/queue", "/users"});
        registry.setApplicationDestinationPrefixes(new String[]{"/ws/wsocket"});
        registry.setUserDestinationPrefix("/user");
    }

    public void configureWebSocketTransport(WebSocketTransportRegistration registry) {
        configureWebSocketTransport(registry);
    }
}

